package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qs.mapper.GoodsMapper;
import com.qs.model.entity.Category;
import com.qs.mapper.CategoryMapper;
import com.qs.model.entity.Goods;
import com.qs.model.vo.CategoryGoodsVo;
import com.qs.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类管理表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-02
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    private final GoodsMapper goodsMapper;

    @Override
    public List<CategoryGoodsVo> getGoodsWithCategory(String category) {
        Page<Goods> pageInfo = new Page<>(1, 30);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("category", category);
        wrapper.orderByDesc("create_time");
        Page<Goods> goodsPage = goodsMapper.selectPage(pageInfo, wrapper);
        if(null != goodsPage){
            List<Goods> goods = goodsPage.getRecords();
            if(!CollectionUtils.isEmpty(goods)){
                List<CategoryGoodsVo> list = new ArrayList<>();
                goods.stream().forEach(item -> {
                    CategoryGoodsVo categoryGoodsVo = new CategoryGoodsVo();
                    categoryGoodsVo.setId(item.getId());
                    categoryGoodsVo.setName(item.getName());
                    categoryGoodsVo.setImage(item.getImage());
                    list.add(categoryGoodsVo);
                });
                return list;
            }
        }
        return null;
    }
}
